package com.tbsoft.trail.bean;

import java.util.List;

import org.apache.camel.Exchange;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.core.process.AbstractProcess;
import com.tbsoft.trail.model.PurchaseOrder;
import com.tbsoft.trail.service.PurchaseOrderService;
import com.tbsoft.trail.util.XmlParse;
/**
 * @desc 订单合成采购单处理bean
 * @author lizhenhua
 *
 */
public class XmlResolveBean extends AbstractProcess {

	private static final Log log = LogFactory.getLog(XmlResolveBean.class);

	private PurchaseOrderService purchaseOrderService;

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	@Override
	public void execute(Exchange exchange) throws Exception {
		String xml = exchange.getIn().getBody(String.class);
		log.info("传入得到的xml：" + xml);
		// 此处通过xml2pojo实现拆分xml
		XmlParse xmlParse = XmlParse.getInstance();
		List<PurchaseOrder> orders = xmlParse.xmlToBean(PurchaseOrder.class,
				xml, "UTF-8");
		for(PurchaseOrder order:orders){
			log.info(order.toString());
		}
		// 把PurchaseOrder保存到数据表中
		List<Integer> orderKeys = this.getPurchaseOrderService()
				.savePurchaseOrders(orders);
		exchange.getOut().setBody(orderKeys);
		String correlateId=exchange.getIn().getHeader(MSG_CORRELATE_ID,String.class);
		if("12306".equals(correlateId)){
			Thread.sleep(3000);
		}else if ("12307".equals(correlateId)){
			Thread.sleep(10000);
		}
	}

}
